library RifleGrenade initializer init requires LegacySystem


	private function RifleGrenadeConditions takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i=='A02A' or i=='A038' or i=='A00U' or i=='A026'
	endfunction

	private function RifleGrenadeArea takes nothing returns nothing
		call SetDestructableLife( GetEnumDestructable(), GetDestructableLife(GetEnumDestructable()) - (500.0+I2R(udg_myint)*250.0) )
	endfunction

	private function RifleGrenade takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local location l = GetSpellTargetLoc()
		local real x = GetLocationX(l)
		local real y = GetLocationY(l)
		local rect r = Rect( x-275, y-275, x+275, y+275 )
		
		call GernadeStunAOE(x,y)
		call AttachSoundToUnit(gg_snd_RifleGrenade,u)
		call SetSoundVolume(gg_snd_RifleGrenade,97)
		call StartSound(gg_snd_RifleGrenade)
		call EarShockArea(x,y,500)
		call EnumItemsInRect(r,null,function killitem)
		set udg_myint = GetUnitAbilityLevel(GetTriggerUnit(),'AHfs')
		call EnumDestructablesInRectAll( r, function RifleGrenadeArea )
		call RemoveRect(r)
		set r = null
		call RemoveLocation(l)
		set l = null
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t, Condition( function RifleGrenadeConditions ) )
		call TriggerAddAction(t,function RifleGrenade)
	endfunction

endlibrary